package socketinterface;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;

import socketinterface.commands.ICommandPacket;
import socketinterface.commands.IoProtocol;
import socketinterface.commands.IoRefreshrate;
import socketinterface.commands.IoUpdate;
import socketinterface.requests.ClientRequest;

import com.vast.ui.iopanel.views.ViewPart;

public class BlockSocketClient extends Thread {
	public  ViewPart      view;
	private Logger        logger;
	private ClientRequest clientRequest;
	private final ThreadFactory threadFactory;
	private ICommandPacket command;
	
	public BlockSocketClient(ViewPart view, ClientRequest clientRequest, Logger logger, ThreadFactory threadFactory) {
		this.view          = view;
		this.logger        = logger;
		this.clientRequest = clientRequest;
		this.threadFactory = threadFactory;
	}
	
	public void sendIoUpdate(int index, boolean state){
		command = new IoUpdate(clientRequest, index, state);
		dispatchCommand();
	}
	
	public void sendIoRefreshrate(int refreshRate){
		command = new IoRefreshrate(clientRequest, refreshRate);
		dispatchCommand();
	}
	
	public void dispatchCommand() {
		threadFactory.newThread(new Runnable() {
			public void run(){
				// TODO Auto-generated method stub
				try {
					command.execute();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
}
